/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ufcg.lvc.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 
 * Classe que faz a conexão com o Bando de Dados, assim como consultas e updates no mesmo.
 * 
 * @author Lari Costa
 * 
 */
public class Conexao {

	String driver = "org.postgresql.Driver";
	String url = "jdbc:postgresql://localhost:5432/CorrigeFacilDB";
	String usuario = "postgres";
	String senha = "lari00";//123
	Connection conexao;
	Statement stetement;
	ResultSet resultSet;
	String valido = "";

	public boolean conecta() {
		boolean result = true;
		try {
			Class.forName(driver);
			conexao = DriverManager.getConnection(url, usuario, senha);
		} catch (ClassNotFoundException e) {
			result = false;
		} catch (SQLException e) {
			result = false;
		}
		return result;
	}

	public void desconecta() {
		try {
			conexao.close();
		} catch (Exception e) {
		}
	}

	public void executaConsulta(String sql) {
		try {
			stetement = conexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			resultSet = stetement.executeQuery(sql);
		} catch (SQLException ex) {
		}
	}

	public void executaUpdate(String sql) {
		try {
			stetement = conexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			stetement.executeUpdate(sql);		
		} catch (SQLException ex) {
		}
	}
	
	public Connection getConexao() {
		return conexao;
	}

	public static Conexao getNewConexao() {
		Conexao conexaoUsuario = new Conexao();
		conexaoUsuario.conecta();
		return conexaoUsuario;
	}

	public ResultSet getResultSet() {
		return resultSet;
	}

	public Statement getStetement() {
		return stetement;
	}

	public String getValido() {
		return valido;
	}
}
